<?php
/*      Database Component      */
require_once 'vn.core.php';

class VNDatabase extends VNCore {

	protected       $VNcore;
	private         $link;

	public function VNDatabase(){

		try{

			$settings = VNCore::getSettings();

			$this->VNcore = array(	"__VNSite" 	=> $settings['VNSite'],
						"__VNName"	=> $settings['VNName'],
						"__VNTime" 	=> $settings['VNTime'],
						"__VNDebug" 	=> $settings['VNDebug']);

			if(empty($this->VNcore) || empty($settings)){

				throw new VNException('Cannot Initiate VN Core',200,__CLASS__);

			}

			/*      Get database settings   */
			$db	= $settings['dbname'];
			$host	= $settings['dbhost'];
			$user	= $settings['dbusername'];
			$pass	= $settings['dbpassword'];

			/*      Establish a connection to the database  */
			$this->link = mysql_connect($host, $user, $pass);

			if(!$this->link){
				throw new VNException("Cannot Connect To Database",201,__CLASS__);
			}

			mysql_select_db($db, $this->link);
			//register_shutdown_function(array(&$this, 'close'));

		}catch (VNException $e){

			print $e;
			die();

		}

	}
	
	protected function switchdb($str=false){
		
		if(empty($str)){
			$settings = VNCore::getSettings();
			mysql_select_db($settings['dbname'],$this->link) or die("Can't connect to database.");
			return true;
		}
		if($str == "dbperms"){
			mysql_select_db(mysql_real_escape_string('mysql'),$this->link);
			return true;
		}
		return false;
	}

	protected function query($query){

		return mysql_query($query, $this->link);

	}

	protected function affectedRows(){

		return mysql_affected_rows();

	}
	
	protected function fetchArray($result){

		return mysql_fetch_array($result);

	}

	protected function fetchAssoc($result){

		return mysql_fetch_assoc($result);

	}

	protected function numRows($result){

		return mysql_num_rows($result);

	}

	protected function fetchRow($result){

		return mysql_fetch_row($result);

	}

	protected function close() {

		mysql_close($this->link);

	}
	
	protected function update($uArr,$where=NULL){
		
		foreach($uArr['assoc'] as $key => $value){
			
			$qKey .= "`".$key."` = '".$value."',";
			
		}
		
		$q = sprintf("UPDATE %s SET %s",
						$uArr['table'],
						substr($qKey, 0, -1)
					);
					
		if(!empty($where)){
			
			$q .= " WHERE `".$where['key']."` = '".$where['value']."'";
			
		}
		$r = $this->query($q) or print __CLASS__." [600] Error in query <br/>".mysql_error();
		
		if($r){
			
			return true;
			
		}
		
		return false;
		
	}
	
	
	protected function delete($dArr){
		
		$q = sprintf("DELETE FROM %s WHERE %s LIMIT 1",
				$dArr['table'],
				$dArr['delete']);
				
		$r = $this->query($q) or print __CLASS__." [600] Error in query <br/>".mysql_error();
		
		if($r){
			
			return true;
			
		}
		
		return false;
				
	}
	
	protected function create($cArr){
		
		$q = sprintf("INSERT INTO %s (%s) VALUES (%s);",
				$cArr['table'],
				$cArr['fields'],
				$cArr['values']);
						
		$r = $this->query($q) or print __CLASS__." [600] Error in query <br/>".mysql_error()."<br/>".$q."<br/>";
		
		if($r){
			
			return true;
			
		}
		
		return false;
		
	}
	
	protected function cleanAutoGenQuery($string){
		
		return substr($string, 0, -1);
		
	}
	
	protected function lastInsertID(){
		
		return mysql_insert_id();
		
	}

	protected function transactionStart(){
		
		$q = "START TRANSACTION;";
		$r = $this->query($q) or print __CLASS__." [600] Error in query <br/>".mysql_error()."<br/>".$q."<br/>";
		
		if($r){
			
			return true;
			
		}
		
		return false;

	}
	
	protected function transactionCommit(){
		
		$q = "COMMIT;";
		$r = $this->query($q) or print __CLASS__." [600] Error in query <br/>".mysql_error()."<br/>".$q."<br/>";
		
		if($r){
			
			return true;
			
		}
		
		return false;
				
	}
	
	protected function transactionRollback(){
		
		$q = "ROLLBACK;";
		$r = $this->query($q) or print __CLASS__." [600] Error in query <br/>".mysql_error()."<br/>".$q."<br/>";
		
		if($r){
			
			return true;
			
		}
		
		return false;
		
	}
	
}
?>
